package com.villaday.idf.adapter3;

import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.villaday.idf.R;
import com.villaday.idf.model.MaterialRecommendEntity;

import java.util.List;


/**
 * Created by yarolegovich on 07.03.2017.
 */

public class ShopAdapter extends RecyclerView.Adapter<ShopAdapter.ViewHolder> {

    private List<MaterialRecommendEntity.BodyEntity.DataListEntity.ImageListEntity> data;
    private View.OnClickListener onClickListener;

    public ShopAdapter(List<MaterialRecommendEntity.BodyEntity.DataListEntity.ImageListEntity> data,View.OnClickListener onClickListener) {
        this.data = data;
        this.onClickListener = onClickListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View v = inflater.inflate(R.layout.item_img, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        String imageUrl = data.get(position).getImageUrl();
        if(!imageUrl.equals(holder.itemView.getTag())){//解决图片加载不闪烁的问题,可以在加载时候，对于已经加载过的item,  采用比对tag方式判断是否需要重新计算高度

            holder.itemView.setTag(null);
            Glide.with(holder.itemView.getContext())
                    .load(data.get(position).getImageUrl())
                    .into(holder.image);
            //if (holder.itemView.getTag()==null){
            holder.itemView.setTag(imageUrl);
            holder.itemView.setOnClickListener(onClickListener);
            // }
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView image;

        public ViewHolder(View itemView) {
            super(itemView);
            image = (ImageView) itemView.findViewById(R.id.img);
        }
    }
}
